package com.jarvis.app.car.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jarvis.app.car.R;
import com.jarvis.app.car.model.Car;
import com.jarvis.app.car.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class StaffItemAdapter extends RecyclerView.Adapter<StaffItemAdapter.StaffAdapterHolder> {

    private List<User> userList = new ArrayList<>();

    private ItemViewOnClickListener itemViewOnClickListener;

    private Activity parent;


    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
        //一定要刷新
        notifyDataSetChanged();
    }

    public void setItemViewOnClickListener(ItemViewOnClickListener itemViewOnClickListener) {
        this.itemViewOnClickListener = itemViewOnClickListener;
    }

    public StaffItemAdapter(Activity parent) {
        this.parent = parent;
    }

    @NonNull
    @Override
    public StaffAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.staff_list_item, null);
        return new StaffItemAdapter.StaffAdapterHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull StaffAdapterHolder holder, int position) {
        //绑定数据
        User user = getUserList().get(position);

        holder.tv_name.setText(user.getName());
        holder.tv_phone.setText(user.getPhone());

        Glide.with(parent)
                .load(user.getImg())
                .into(holder.iv_staffImg);

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (Objects.nonNull(itemViewOnClickListener)) {
                    itemViewOnClickListener.deleteOnLongClick(user, position);
                }
                return true;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Objects.nonNull(itemViewOnClickListener)) {
                    itemViewOnClickListener.editOnClick(user, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return getUserList().size();
    }

    static class StaffAdapterHolder extends RecyclerView.ViewHolder {


        ImageView iv_staffImg;

        TextView tv_name;

        TextView tv_phone;


        public StaffAdapterHolder(@NonNull View itemView) {
            super(itemView);
            iv_staffImg = itemView.findViewById(R.id.iv_staffImg);

            tv_name = itemView.findViewById(R.id.tv_name);

            tv_phone = itemView.findViewById(R.id.tv_phone);

        }
    }

    public interface ItemViewOnClickListener {
        void deleteOnLongClick(User user, int position);


        void editOnClick(User user,int position);
    }
}
